<?php
class Application_Model_Catalog
{
    protected $_type = 'Core_Model_FullPathCatalog';
    
    protected $_storage = null;
	 
	 protected $encodeMeta = array(
		  'use' => true,
		  'name_postfix' => '_enc',
		  'salt' => '456',
		  'fields' => array(
				'title',
				'text'
		  ),
	 );


	 //protected $_data = null;
    
    public function __construct( $root = 0 )
    {
		 //$class = $this->_type;
		 //$this->_storage = $class::getInstance( $root );
		 
		$this->_storage = new $this->_type( $root );
		
		if ( $this->encodeMeta['use'] )
		{
			$fdata = $encodedFieldsNames = array();
			foreach ( $this->encodeMeta['fields'] as $fname )
			{
				$fdata[] = new Zend_Db_Expr( "AES_DECRYPT(".($fname.$this->encodeMeta['name_postfix']).",'".$this->encodeMeta['salt']."') as ".$fname );
				$encodedFieldsNames[] = $fname.$this->encodeMeta['name_postfix'];
			}
			//$this->checkEncodedFieldsExists( $encodedFieldsNames );
			$this->_storage->setSelectFields( $fdata );
		}

		//echo '<xmp>'.print_r($this->_storage, 1).'</xmp>'; exit;
		
		//$this->_data = $this->_storage->init( $root );
		
	
    }
    
	public function __call( $method, $arg = array() )
	{
		$return = null;
		if ( method_exists( $this->_storage, $method ) ) {
			$return = call_user_func_array( array( $this->_storage, $method ), $arg );
		}
		return $return;
	}
	
	public function __get( $attrName )
	{
		return $this->_storage->$attrName;
	}
/*	
	protected function checkEncodedFieldsExists( $encodedFieldsNames )
	{
		$fieldsMeta = $this->_storage->getFieldsMetaData();
		$fieldsMeta = array_keys( $fieldsMeta );
		$diff = array_diff( $encodedFieldsNames, $fieldsMeta );
		if ( !empty ( $diff ) ){
			throw new Zend_Db_Exception( 'No DB fields for encoded data' );
		}
	}
*/
	public function insert( $data )
	{
		
		$this->prepareData( $data );
		
		$return = null;
		if ( !empty ( $data['id'] ) ) {
			$this->_storage->update( $data );
		} else {
			$this->_storage->insert( $data );
		}
		return $return;
	}
	
	protected function prepareData( &$data )
	{
		if ( $this->encodeMeta['use'] )
		{
			foreach ( $this->encodeMeta['fields'] as $fname )
			{
				$data[$fname . $this->encodeMeta['name_postfix']] = new Zend_Db_Expr("AES_ENCRYPT('".$data[$fname]."','".$this->encodeMeta['salt']."')" );
				$data[$fname] = '';
			}
		}
	}
	
	public function contextSearch( $searchStr )
	{
		$return = array();

		$this->setWhere( $this->prepareWhereString( $this->prepareContextWhere( $searchStr ) ) );
		
		$this->initTree();
		$return = $this->getPlainTree();

		return $return;
	}
	
	protected function prepareContextWhere( $searchStr )
	{
		$return = array();
		foreach ( $this->encodeMeta['fields'] as $fname )
		{
			if ( $this->encodeMeta['use'] ) {
				$return[] = "AES_DECRYPT(".$fname . $this->encodeMeta['name_postfix'].", '".$this->encodeMeta['salt']."') like '%".$searchStr."%' ";
			} else 
			{
				$return[] = $fname." like '%".$searchStr."%' ";
			}
		}
		return $return;
	}
	
	protected function prepareWhereString( $whereArr )
	{
		return implode( ' OR ', $whereArr );
	}
   
}